﻿'***********************************************************************
'Magic Chimney Imp (MCI)
'Copyright (C) 2011  David Frendin (david.frendin@gmail.com)
'
'This program is free software: you can redistribute it and/or modify
'it under the terms of the GNU General Public License as published by
'the Free Software Foundation, either version 3 of the License, or
'(at your option) any later version.
'
'This program is distributed in the hope that it will be useful,
'but WITHOUT ANY WARRANTY; without even the implied warranty of
'MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
'GNU General Public License for more details.
'
'You should have received a copy of the GNU General Public License
'along with this program.  If not, see <http://www.gnu.org/licenses/>.
'***********************************************************************
Public Class frmPlayTest
    Private DeckObject As Deck
    Private CardQueue As Queue(Of Card)

    Private Sub frmPlayTest_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        KryptonPanelBattlefield.AllowDrop = True
        KryptonPanelGraveyard.AllowDrop = True
        'PictureBox2.MouseDown += new System.Windows.Forms.MouseEventHandler(this. btnImage_MouseDown);
        'this.picBox.DragDrop += new System.Windows.Forms.DragEventHandler(this.pictureBox_DragDrop);
        'this.picBox.DragEnter += new System.Windows.Forms.DragEventHandler(this.pictureBox_DragEnter);
    End Sub

    Public Sub LoadDeck(ByVal d As Deck)
        DeckObject = d

        Dim deckarray As New ArrayList
        For Each cs As Deck.CardSlot In DeckObject.MainDeck
            For i As Integer = 1 To cs.Quantity
                deckarray.Add(cs.Card)
            Next
        Next

        deckarray = Shuffle(deckarray)

        CardQueue = New Queue(Of Card)
        For Each c As Card In deckarray
            CardQueue.Enqueue(c)
        Next
    End Sub

    Private Sub PictureBox2_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
        If e.Button = MouseButtons.Left And e.Clicks = 1 Then
            Dim btnPic As PictureBox = CType(sender, PictureBox)
            btnPic.DoDragDrop(btnPic, DragDropEffects.Move)
        End If
    End Sub

    Private Sub PictureBox2_MouseHover(ByVal sender As Object, ByVal e As EventArgs)
        Dim pbx As PictureBox = DirectCast(sender, PictureBox)

        PictureBoxCardLarge.Image = pbx.Image.Clone

        If pbx.Width = 103 Then
            PictureBoxCardLarge.Image.RotateFlip(RotateFlipType.Rotate270FlipNone)
        End If
    End Sub
    'PictureBoxCardLarge

    Private Sub KryptonPanelBattlefield_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles KryptonPanelBattlefield.DragDrop
        Dim pbx As PictureBox = DirectCast(e.Data.GetData(GetType(PictureBox)), PictureBox)
        Dim TheCard As Card = CType(pbx.Tag, Card)

        AddToLog("Put " & TheCard.Name & " to battlefield")
        Dim pt As Point = KryptonPanelBattlefield.PointToClient(New Point(e.X, e.Y))

        pt.X = CInt(Math.Floor(pt.X / (75 * 0.5))) * (75 * 0.5)
        pt.Y = CInt(Math.Floor(pt.Y / 103)) * 103

        pbx.Left = pt.X
        pbx.Top = pt.Y

        pbx.Dock = DockStyle.None
        KryptonPanelBattlefield.Controls.Add(pbx)
    End Sub

    Private Sub KryptonPanelGraveyard_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles KryptonPanelGraveyard.DragDrop
        Dim pbx As PictureBox = DirectCast(e.Data.GetData(GetType(PictureBox)), PictureBox)
        AddToLog("Put " & CType(pbx.Tag, Card).Name & " to graveyard")
        pbx.Dock = DockStyle.Fill
        KryptonPanelGraveyard.Controls.Add(pbx)
        pbx.SendToBack()
    End Sub

    Private Sub KryptonPanelStack_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles KryptonPanelStack.DragDrop
        Dim pbx As PictureBox = DirectCast(e.Data.GetData(GetType(PictureBox)), PictureBox)
        AddToLog("Put " & CType(pbx.Tag, Card).Name & " to stack")
        pbx.Dock = DockStyle.Left
        KryptonPanelStack.Controls.Add(pbx)
        'pbx.SendToBack()
    End Sub

    Private Sub KryptonPanelHand_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles KryptonPanelHand.DragDrop
        Dim pbx As PictureBox = DirectCast(e.Data.GetData(GetType(PictureBox)), PictureBox)
        AddToLog("Put " & CType(pbx.Tag, Card).Name & " to hand")
        pbx.Dock = DockStyle.Left
        KryptonPanelHand.Controls.Add(pbx)
        pbx.SendToBack()
    End Sub

    Private Sub KryptonPanelExile_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles KryptonPanelExile.DragDrop
        Dim pbx As PictureBox = DirectCast(e.Data.GetData(GetType(PictureBox)), PictureBox)
        AddToLog("Put " & CType(pbx.Tag, Card).Name & " to exile")
        pbx.Dock = DockStyle.Fill
        KryptonPanelExile.Controls.Add(pbx)
        pbx.SendToBack()
    End Sub

    Private Sub KryptonPanelGraveyard_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles KryptonPanelGraveyard.DragEnter
        e.Effect = DragDropEffects.Move
    End Sub

    Private Sub KryptonPanelStack_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles KryptonPanelStack.DragEnter
        e.Effect = DragDropEffects.Move
    End Sub

    Private Sub KryptonPanelBattlefield_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles KryptonPanelBattlefield.DragEnter
        Dim pbx As PictureBox = DirectCast(e.Data.GetData(GetType(PictureBox)), PictureBox)
        Dim TheCard As Card = CType(pbx.Tag, Card)
        If TheCard.Type.Contains("Instant") Or TheCard.Type.Contains("Sorcery") Then
            e.Effect = DragDropEffects.None
        Else
            e.Effect = DragDropEffects.Move
        End If
    End Sub

    Private Sub KryptonPanelHand_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles KryptonPanelHand.DragEnter
        e.Effect = DragDropEffects.Move
    End Sub

    Private Sub KryptonPanelExile_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles KryptonPanelExile.DragEnter
        e.Effect = DragDropEffects.Move
    End Sub

    Private Sub PictureBoxDeck_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles PictureBoxDeck.DoubleClick
        DrawCards(1)
    End Sub


    Public Function Shuffle(ByVal Queue As Queue(Of Card)) As Queue(Of Card)
        Dim ArrayList As New ArrayList
        While Queue.Count > 0
            ArrayList.Add(Queue.Dequeue)
        End While

        ArrayList = Shuffle(ArrayList)

        For i As Integer = 0 To ArrayList.Count - 1
            Queue.Enqueue(ArrayList(i))
        Next

        AddToLog("You shuffle your library")

        Return Queue
    End Function

    Public Function Shuffle(ByVal Array As ArrayList) As ArrayList
        Dim intUBound As Integer
        Dim intHUBound As Integer
        Dim intRanNum As Integer
        Dim a As Object
        Dim b As Object
        Dim i As Integer


        'Starts the randon deck Shuffle
        intUBound = Array.Count - 1
        intHUBound = intUBound

        ReDim a(intUBound)
        ReDim b(intUBound)

        'On Error GoTo e

        For i = 0 To intUBound
            a(i) = Array(i) ' Sets array (a) list to (1 to cntnum)

        Next i

        Do

            Randomize() ' Activates the Random Number Generator
            intRanNum = Int(Rnd() * intUBound) ' Picks a Random number between 0 and max number
            b(0) = a(intRanNum)

            Try
                For i = 1 To intUBound ' Starts a loop
                    If intRanNum = intUBound Then intRanNum = -1 ' If the Random number = max number then the Random number becomes 0
                    intRanNum += 1  ' Adds 1 to the Random number
                    b(i) = a(intRanNum) ' b(current loop value) = The current Random number

                Next i ' Adds 1 to i and loops until i is greater than the max Number

                For i = 0 To intUBound
                    a(i) = b(i) ' Makes array a the same as array b
                Next i

                intUBound -= 1 ' Subtracts 1 from the max number
            Catch
                'If any error has occured the user is warned
                MsgBox("An Error has occured, Outcome may not be acurate", MsgBoxStyle.Critical, "Error")
            End Try
        Loop Until intUBound = -1

        Dim result As New ArrayList
        For i2 As Integer = 0 To Array.Count - 1
            result.Add(a(i2))
        Next
        Return result
    End Function

    Private Sub Draw7CardsToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Draw7CardsToolStripMenuItem.Click
        DrawCards(7)
    End Sub

    Private Sub DrawCards(ByVal Count As Integer)
        'draw a card
        For i As Integer = 1 To Count
            If Not CardQueue.Count = 0 Then
                Dim NewPictureBox As New PictureBox
                Dim NewLabel As New Label
                Dim CardInfo As Card = CType(CardQueue.Dequeue, Card)
                AddToLog("You draw a " & CardInfo.Name)
                NewPictureBox.Tag = CardInfo
                NewLabel.Text = CardInfo.Name
                NewLabel.Dock = DockStyle.Top
                NewLabel.AutoSize = False
                NewLabel.BackColor = Color.Black
                NewLabel.ForeColor = Color.White
                CardPool.SetCardImage(NewPictureBox, CardInfo.MultiverseId)
                NewPictureBox.Dock = DockStyle.Left
                NewPictureBox.SizeMode = PictureBoxSizeMode.StretchImage
                NewPictureBox.Width = 75
                NewPictureBox.Height = 103
                NewPictureBox.Controls.Add(NewLabel)
                'NewPictureBox.

                AddHandler NewPictureBox.MouseDown, AddressOf PictureBox2_MouseDown
                AddHandler NewPictureBox.MouseHover, AddressOf PictureBox2_MouseHover
                AddHandler NewPictureBox.MouseDoubleClick, AddressOf PictureBox2_MouseDbl


                KryptonPanelHand.Controls.Add(NewPictureBox)
                NewPictureBox.ContextMenuStrip = ContextMenuStripCard

                If CardQueue.Count = 0 Then
                    PictureBoxDeck.Visible = False
                End If
            End If
        Next
    End Sub

    Protected Sub PictureBox2_MouseDbl(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Dim pbx As PictureBox = DirectCast(sender, PictureBox)
        TapCard(pbx)
    End Sub

    Private Sub DrawACardToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DrawACardToolStripMenuItem.Click
        DrawCards(1)
    End Sub

    Private Sub ShuffleLibraryToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ShuffleLibraryToolStripMenuItem.Click
        CardQueue = Shuffle(CardQueue)
    End Sub

    Private Sub LibraryToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LibraryToolStripMenuItem.Click
        Dim dlg As New dlgViewDeck

        AddToLog("You look at your library")

        For i As Integer = 0 To CardQueue.Count - 1
            dlg.KryptonListBoxDeck.Items.Add(CardQueue(i).Name)
        Next
        dlg.ShowDialog(Me)

        If dlg.KryptonCheckBoxShuffle.Checked Then
            CardQueue = Shuffle(CardQueue)
        End If
    End Sub

    Private Sub ResetToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ResetToolStripMenuItem.Click
        KryptonListBoxLog.Items.Clear()
        AddToLog("Game reset")
        While KryptonPanelHand.Controls.Count > 0
            Dim AsPictureBox As PictureBox = DirectCast(KryptonPanelHand.Controls(0), PictureBox)
            KryptonPanelHand.Controls.Remove(AsPictureBox)
            AsPictureBox.Dispose()
        End While
        While KryptonPanelBattlefield.Controls.Count > 0
            Dim AsPictureBox As PictureBox = DirectCast(KryptonPanelBattlefield.Controls(0), PictureBox)
            KryptonPanelBattlefield.Controls.Remove(AsPictureBox)
            AsPictureBox.Dispose()
        End While
        While KryptonPanelGraveyard.Controls.Count > 0
            Dim AsPictureBox As PictureBox = DirectCast(KryptonPanelGraveyard.Controls(0), PictureBox)
            KryptonPanelGraveyard.Controls.Remove(AsPictureBox)
            AsPictureBox.Dispose()
        End While
        While KryptonPanelExile.Controls.Count > 0
            Dim AsPictureBox As PictureBox = DirectCast(KryptonPanelExile.Controls(0), PictureBox)
            KryptonPanelExile.Controls.Remove(AsPictureBox)
            AsPictureBox.Dispose()
        End While
        While KryptonPanelStack.Controls.Count > 0
            Dim AsPictureBox As PictureBox = DirectCast(KryptonPanelStack.Controls(0), PictureBox)
            KryptonPanelStack.Controls.Remove(AsPictureBox)
            AsPictureBox.Dispose()
        End While
        LoadDeck(DeckObject)
    End Sub

    Private Sub TapToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TapToolStripMenuItem.Click
        Dim pbx As PictureBox = DirectCast(DirectCast(sender, System.Windows.Forms.ToolStripMenuItem).Owner, System.Windows.Forms.ContextMenuStrip).SourceControl
        Dim TheCard As Card = CType(pbx.Tag, Card)

        If TheCard.Type.Contains("Instant") Or TheCard.Type.Contains("Sorcery") Or TheCard.Type.Contains("Enchantment") Then 'non-permanents and enchantments can not be tapped
            AddToLog("Card type can not be tapped")
            Exit Sub
        End If
        TapCard(pbx)
    End Sub

    Private Sub TapCard(ByVal pbx As PictureBox)
        If pbx.Width = 103 Then
            pbx.Width = 75
            pbx.Height = 103
            pbx.Image.RotateFlip(RotateFlipType.Rotate270FlipNone)
            AddToLog("You untap " & CType(pbx.Tag, Card).Name)
        Else
            pbx.Width = 103
            pbx.Height = 75
            pbx.Image.RotateFlip(RotateFlipType.Rotate90FlipNone)
            AddToLog("You tap " & CType(pbx.Tag, Card).Name)
        End If
        pbx.Refresh()
    End Sub

    Private Sub AddToLog(ByVal str As String)
        KryptonListBoxLog.Items.Add(str)
    End Sub

    Private Sub DrawACardToolStripMenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DrawACardToolStripMenuItem1.Click
        DrawCards(1)
    End Sub

    Private Sub CloseToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CloseToolStripMenuItem.Click
        Me.Close()
    End Sub

    Private Sub TopXCardsToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TopXCardsToolStripMenuItem.Click
        Using dlg As New dlgSelectNumber
            dlg.KryptonNumericUpDownNumber.Value = 1
            Dim dlgResult As DialogResult = dlg.ShowDialog(Me)

            If dlgResult = DialogResult.OK Then
                Using dlgLibrary As New dlgViewDeck
                    If dlg.KryptonNumericUpDownNumber.Value > CardQueue.Count Then
                        AddToLog("You don't have " & dlg.KryptonNumericUpDownNumber.Value.ToString & " cards in your library")
                        Exit Sub
                    End If
                    AddToLog("You look at the top " & dlg.KryptonNumericUpDownNumber.Value.ToString & " cards of your library")

                    For i As Integer = 0 To dlg.KryptonNumericUpDownNumber.Value - 1
                        dlgLibrary.KryptonListBoxDeck.Items.Add(CardQueue(i).Name)
                    Next
                    dlgLibrary.ShowDialog(Me)

                    If dlgLibrary.KryptonCheckBoxShuffle.Checked Then
                        CardQueue = Shuffle(CardQueue)
                    End If
                End Using
            End If
        End Using
    End Sub

    Private Sub AnnotateToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AnnotateToolStripMenuItem.Click
        Using dlg As New dlgAnnotateCard
            dlg.KryptonTextBoxText.Text = ""
            Dim dlgResult As DialogResult = dlg.ShowDialog(Me)

            If dlgResult = DialogResult.OK Then
                Dim pbx As PictureBox = DirectCast(DirectCast(sender, System.Windows.Forms.ToolStripMenuItem).Owner, System.Windows.Forms.ContextMenuStrip).SourceControl
                For Each Control As Control In pbx.Controls
                    If TypeOf (Control) Is Label Then
                        Dim clbl As Label = DirectCast(Control, Label)
                        If clbl.Tag = "annotation" Then
                            clbl.Text = dlg.KryptonTextBoxText.Text
                            Exit Sub
                        End If
                    End If
                Next

                Dim NewLabel As New Label
                NewLabel.Tag = "annotation"
                NewLabel.Text = dlg.KryptonTextBoxText.Text
                NewLabel.Dock = DockStyle.Bottom
                NewLabel.AutoSize = False
                NewLabel.BackColor = Color.Black
                NewLabel.ForeColor = Color.White
                pbx.Controls.Add(NewLabel)

            End If
        End Using
    End Sub
End Class